﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace KgUtils
{
    public static partial class EnumHelper
    {
        /// <summary>
        /// Parses an Enum value. It improves the existing by the fact that you don't have to cast the result
        /// </summary>
        /// <typeparam name="T">Target Enum type</typeparam>
        /// <param name="value">The value to parse</param>
        /// <returns></returns>
        [DebuggerStepThrough]
        public static T Parse<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value);
        }

        /// <summary>
        /// Convertit une liste de flags en une combinaison de ces flags
        /// </summary>
        /// <typeparam name="T">Le type de l'énumération</typeparam>
        /// <param name="flags">La liste de flags à combiner</param>
        /// <returns>La combinaison des flags de la liste</returns>
        public static T CombineFlags<T>(IEnumerable<T> flags) where T : struct
        {
            CheckIsEnum<T>();

            T tmp = default(T);
            foreach (T flag in flags)
            {
                tmp = tmp.SetFlags(flag, true);
            }
            return tmp;
        }


        internal static bool CheckIsEnum<T>()
        {
            if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type");
            return true;
        }
    }
}
